package gui;

import java.awt.event.*;
import javax.swing.JInternalFrame;
import controllers.GUIController;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class AEditUser extends JInternalFrame {

    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JButton addstudentButton;
    private javax.swing.JPanel bkgrd;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField firstNameField;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JButton homeButton;
    private javax.swing.JTextField lastNameField;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JLabel logo;
    private javax.swing.JPanel logoPanel;
    private javax.swing.JButton logoutButton;
    private javax.swing.JButton myaccountButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JPanel userInfoPanel;
    private JButton activitiesButton;
    private GUIController guiC;
    final AEditUser obj = this;

    public AEditUser(GUIController m) {
        guiC = m;
        generateLayout();
        addLogoutActionListener();
        addHomeActionListener();
        addMyAccountActionListener();
        addCancelActionListener();
        addStudentActionListener();
        addActivitiesActionListener();
        addSaveActionListener();
    }
    
     private void addActivitiesActionListener() {
        activitiesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AAddActivity(guiC));
                obj.dispose();
            }
        });
    }
     
     
    private void addSaveActionListener() {
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.currentUser.setFirstName(firstNameField.getText());
                guiC.currentUser.setLastName(lastNameField.getText());
                guiC.saveDbData(guiC.currentUser);
            }
        });
    }

    // adds actionlistener to the logout button to log the admin out of their
    // account
    private void addLogoutActionListener() {
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // show login page/close application on logout
                guiC.showGUI(new ALogin(guiC));
                // close previous gui(AMyClass)
                obj.dispose();
            }
        });
    }

    // adds actionlistener to myaccount button to go to the page My Class
    private void addMyAccountActionListener() {
        myaccountButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    guiC.showGUI(new AMyAccount(guiC));
                } catch (IOException ex) {
                    Logger.getLogger(AEditUser.class.getName()).log(Level.SEVERE, null, ex);
                }
                obj.dispose();
            }
        });
    }

    //adds actionlistener to home button to go to the home page
    private void addHomeActionListener() {
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AMyClass(guiC));
                obj.dispose();
            }
        });
    }

    // adds actionlistener to addstudent button to go to the page My Class
    private void addStudentActionListener() {
        addstudentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AAddStudent(guiC));
                obj.dispose();
            }
        });
    }

    //adds actionlistener to cancel button to go to the my account page
    private void addCancelActionListener() {
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    guiC.showGUI(new AMyAccount(guiC));
                } catch (IOException ex) {
                    Logger.getLogger(AEditUser.class.getName()).log(Level.SEVERE, null, ex);
                }
                obj.dispose();
            }
        });
    }
    

    public void generateLayout() {
        bkgrd = new javax.swing.JPanel();
        logoPanel = new javax.swing.JPanel();
        logo = new javax.swing.JLabel();
        ButtonPanel = new javax.swing.JPanel();
        homeButton = new javax.swing.JButton();
        myaccountButton = new javax.swing.JButton();
        logoutButton = new javax.swing.JButton();
        userInfoPanel = new javax.swing.JPanel();
        firstNameLabel = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        firstNameField = new javax.swing.JTextField(guiC.currentUser.getFirstName());
        lastNameLabel = new javax.swing.JLabel();
        lastNameField = new javax.swing.JTextField(guiC.currentUser.getLastName());
        addstudentButton = new javax.swing.JButton();
        activitiesButton = new JButton("Activities");

        bkgrd.setBackground(new java.awt.Color(0, 102, 255));

        setTitle("OnPoint Scheduler- Edit My Account");

        logoPanel.setOpaque(false);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/OnPointlogo2-1.png"))); // NOI18N

        javax.swing.GroupLayout logoPanelLayout = new javax.swing.GroupLayout(logoPanel);
        logoPanel.setLayout(logoPanelLayout);
        logoPanelLayout.setHorizontalGroup(
                logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(logoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logo)
                .addContainerGap(22, Short.MAX_VALUE)));
        logoPanelLayout.setVerticalGroup(
                logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(logo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

        ButtonPanel.setOpaque(false);

        homeButton.setText("Home");
        homeButton.setOpaque(false);

        myaccountButton.setText("MyAccount");
        myaccountButton.setOpaque(false);

        logoutButton.setText("Logout");
        logoutButton.setOpaque(false);

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
                ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(myaccountButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(homeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(activitiesButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(logoutButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        ButtonPanelLayout.setVerticalGroup(
                ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(homeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(activitiesButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myaccountButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logoutButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        userInfoPanel.setBackground(new java.awt.Color(51, 153, 255));
        userInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Teacher Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 1, 14))); // NOI18N

        firstNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        firstNameLabel.setText("First Name: ");

        saveButton.setText("Save");
        saveButton.setOpaque(false);

        cancelButton.setText("Cancel");
        cancelButton.setOpaque(false);

        firstNameField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        lastNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lastNameLabel.setText("Last Name: ");

        lastNameField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N


        addstudentButton.setText("Add Student");
        addstudentButton.setOpaque(false);

        javax.swing.GroupLayout userInfoPanelLayout = new javax.swing.GroupLayout(userInfoPanel);
        userInfoPanel.setLayout(userInfoPanelLayout);
        userInfoPanelLayout.setHorizontalGroup(
                userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(userInfoPanelLayout.createSequentialGroup()
                .addGroup(userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(firstNameLabel)
                .addComponent(lastNameLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(firstNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                .addComponent(lastNameField))
                .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, userInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(userInfoPanelLayout.createSequentialGroup()
                .addGap(0, 315, Short.MAX_VALUE)
                .addComponent(addstudentButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)))
                .addContainerGap()));
        userInfoPanelLayout.setVerticalGroup(
                userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(userInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(firstNameLabel)
                .addComponent(firstNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lastNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lastNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE))
                .addGap(18, 18, 18)
                .addGroup(userInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(cancelButton)
                .addComponent(saveButton)
                .addComponent(addstudentButton))
                .addContainerGap()));

        javax.swing.GroupLayout bkgrdLayout = new javax.swing.GroupLayout(bkgrd);
        bkgrd.setLayout(bkgrdLayout);
        bkgrdLayout.setHorizontalGroup(
                bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(logoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(userInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(109, Short.MAX_VALUE)));
        bkgrdLayout.setVerticalGroup(
                bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(90, 90, 90))
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addComponent(userInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(64, Short.MAX_VALUE)))));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
                layout.createParallelGroup(Alignment.LEADING)
                .addComponent(bkgrd, GroupLayout.DEFAULT_SIZE, 1009, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(Alignment.LEADING)
                .addComponent(bkgrd, GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE));
        getContentPane().setLayout(layout);

        pack();
    }

}
